<?php
namespace app\home\controller;

use think\cache\driver\Memcache;
use think\cache\driver\Memcached;
use think\Controller;
use think\Db;
use think\facade\Cache;
use think\Request;

class Index extends Controller
{
    public function initialize()
    {
        error_reporting(5);
        /*$top_menu = Cache::get('top_menu');
        if(!$top_menu || !is_array($top_menu)){
            //获取头部导航栏信息
            $this->top_menu = Db::name('config')->where('config_desc','=','top_menu')->where('config_pid','<>',0)->order(['config_order'=>'desc'])->select();
            $GLOBALS['tree']['pid'] = 'config_pid';
            $GLOBALS['tree']['id'] = 'config_id';
            $this->top_menu = get_tree($this->top_menu);
            set_leval($this->top_menu);
            Cache::tag('home_')->set('top_menu',$this->top_menu);
            $this->top_menu = Cache::get('top_menu');
        }else{
            $this->top_menu = Cache::get('top_menu');
        }*/
        //头部导航菜单
        $this->top_menu = Cache::get('home_top_menu');

    }

    public function index()
    {
        $mca=['m'=>\think\facade\Request::module(),'c'=>\think\facade\Request::controller(),'a'=>\think\facade\Request::action()];
        $mca['mca'] = $mca['m'].'/'.$mca['c'].'/'.$mca['a'];
        $mca['ca'] = $mca['c'].'/'.$mca['a'];
        $this->assign('mca',$mca);
        $this->assign('top_menu_list',$this->top_menu);

        $config_list = Db::name('config')->where('config_url','in',[$mca['ca'],$mca['c']])->select();
        foreach ($config_list as $key => $item) {
            if(in_array($item['config_desc'],['banner_image'])){
                $banner = $item['config_img'];
            }
            if(in_array($item['config_desc'],['container_1'])){
                $config_extend = json_decode($item['config_extend'],true);
                $container_1 = $config_extend;
                $container_1['data'] = Db::name('media')->where('media_category_id','=',$config_extend['category_id'])->order(['media_order'=>'desc','media_id'=>'desc'])->limit(0,12)->select();
            }
        }
        //轮播图
        $banner = explode(',',$banner);
        $this->assign('banner',$banner);
        $this->assign('config_list',$config_list);
        $this->assign('container_1',$container_1);
        $this->assign('title',lang('index_index'));
        return $this->fetch();
    }

    public function article(Request $request){
        $mca=['m'=>\think\facade\Request::module(),'c'=>\think\facade\Request::controller(),'a'=>\think\facade\Request::action()];
        $mca['mca'] = $mca['m'].'/'.$mca['c'].'/'.$mca['a'];
        $mca['ca'] = $mca['c'].'/'.$mca['a'];
        $this->assign('mca',$mca);
        $this->assign('top_menu_list',$this->top_menu);

        $param = $request->param();
        $id = $param['id'];
        $cate_id = $param['cate_id'];
        if( !in_array($id,['',null,0]) && !in_array($cate_id,['',null,0])){
            $article_ids = Db::name('media')->field('media_id')->where('media_category_id','=',$cate_id)->order(['media_id'=>'desc'])->select();
            $max_len = count($article_ids);
            foreach ($article_ids as $k => $v){
                if($v['media_id'] == $id){
                    $lr['left'] = $article_ids[($k-1)]['media_id'];
                    $lr['right'] = $article_ids[$k+1]['media_id'];
                    ($k<=0) and ($lr['left'] = -1);
                    ($k>=($max_len-1)) and ($lr['right'] = -1);
                    $this->assign('lr',$lr);
                    break;
                }
            }
            $article = Db::table('fwf_media')->alias('a')->field('a.*,m.member_user')
                ->leftJoin('fwf_member m','a.media_member_id = m.member_id')
                ->where('a.media_id','=',$id)
                ->where('a.media_category_id','=',$cate_id)
                ->find();
            if($article && is_array($article)){
                $this->assign('article',$article);
            }
            else{
                $this->error('您访问的内容不存在,将自动返回首页','/');
            }
        }else{
            $this->error('您访问的内容不存在,将自动返回首页','/');
        }
        $this->assign('title',$article['media_title']);
        return $this->fetch();
    }

    //关于我
    public function about(Request $request){
        $mca=['m'=>\think\facade\Request::module(),'c'=>\think\facade\Request::controller(),'a'=>\think\facade\Request::action()];
        $mca['mca'] = $mca['m'].'/'.$mca['c'].'/'.$mca['a'];
        $mca['ca'] = $mca['c'].'/'.$mca['a'];
        $this->assign('mca',$mca);
        $this->assign('top_menu_list',$this->top_menu);


        return $this->fetch();
    }

    //个人简历
    public function resume(Request $request){
        $mca=['m'=>\think\facade\Request::module(),'c'=>\think\facade\Request::controller(),'a'=>\think\facade\Request::action()];
        $mca['mca'] = $mca['m'].'/'.$mca['c'].'/'.$mca['a'];
        $mca['ca'] = $mca['c'].'/'.$mca['a'];
        $this->assign('mca',$mca);
        $this->assign('top_menu_list',$this->top_menu);

        $this->assign('title','个人简历');

        return $this->fetch();
    }

    //栏目文章 列出当前栏目下的文章列表
    public function category_article(Request $request){
        $mca=['m'=>\think\facade\Request::module(),'c'=>\think\facade\Request::controller(),'a'=>\think\facade\Request::action()];
        $mca['mca'] = $mca['m'].'/'.$mca['c'].'/'.$mca['a'];
        $mca['ca'] = $mca['c'].'/'.$mca['a'];
        $this->assign('mca',$mca);
        $this->assign('top_menu_list',$this->top_menu);

        $param = $request->param();
        $cate_id = $param['id'];

        //获取栏目列表（文章模型的,模型表编号）
        $category_list = Db::name('category')->where('cate_type','=',1)->select();
        //获取栏目信息
        $category = [];
        foreach ($category_list as $key => $value) {
            if($cate_id == $value['cate_id']){
                $category = $value;
            }
        }
        //获取该栏目下的文章列表
        $page = $param['page'] ? ($param['page']-1) : 0;
        $page_len = $page['page_len'] ? $page['page_len'] : 10;
        $article_list_count = Db::name('media')->where('media_category_id','=',$cate_id)->count();
        $article_list = Db::name('media')->where('media_category_id','=',$cate_id)->order(['media_id'=>'desc'])->limit(($page*$page_len),$page_len)->select();

        $this->assign('category',$category);
        $this->assign('article_list',$article_list);
        $this->assign('category_list',$category_list);
        $this->assign('title',$category['cate_name']);
        $this->assign('page',$param['page']? $param['page'] : 1);
        $this->assign('article_list_count',$article_list_count);
        return $this->fetch();
    }
}

